cmake_minimum_required(VERSION 3.12)
project(mpitracer)
set(CMAKE_C_STANDARD 99)
set(CMAKE_C_STANDARD_REQUIRED ON)


set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--disable-new-dtags")

function(configure_tracer_target TARGET_NAME)
    target_compile_options(${TARGET_NAME} PRIVATE
        -O3
        -Wall
        -Wextra
        -Wno-unknown-warning-option
        -Wno-unused-command-line-argument
        -Wno-error=old-style-declaration
        -Wno-error=conversion
        -Wno-conversion
        -Wno-error=unused-parameter
        -Wno-error=sign-conversion
        -Wno-error=shadow
        -Wno-error=format-overflow
        -Wno-error=shadow=compatible-local
        -Wno-error=shadow=local
    )
    
    # 移除C++特有选项（如果从父项目继承）
    get_target_property(target_opts ${TARGET_NAME} COMPILE_OPTIONS)
    if(target_opts)
        list(REMOVE_ITEM target_opts
            -Weffc++
            -Wno-non-virtual-dtor
            -Wdelete-non-virtual-dtor
            -Wzero-as-null-pointer-constant
            -Wold-style-cast
            -Wrange-loop-construct
            -Wsuggest-override
            -Wmismatched-new-delete
            -Wreorder
        )
        set_target_properties(${TARGET_NAME} PROPERTIES COMPILE_OPTIONS "${target_opts}")
    endif()
    
    target_link_libraries(${TARGET_NAME} PRIVATE dl pthread)
endfunction()


set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3 -Wall -fPIC")
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

set(SOURCE_FILES src/mpitracer.c)
include_directories(include)
add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES})
set_target_properties(${PROJECT_NAME} PROPERTIES 
    PREFIX ""
    OUTPUT_NAME "mpitracer"
)
configure_tracer_target(${PROJECT_NAME})

add_library(test_mpi SHARED src/test.c)
configure_tracer_target(test_mpi)

add_subdirectory(mpi_tests)
#  gcc -fPIC -shared -o mpitracer.so mpitracer.c -ldl -lpthread -O3 -Wall